
$(function () {
    const $phone = $("input:eq(0)");        // 要查找的

    const $name = $("input:eq(1)");   // 查找到的内容
    const $passwd = $("input:eq(2)");   //
    const $email = $("input:eq(3)");   //
    const $phone1 = $("input:eq(4)");   //
    const $regtime = $("input:eq(5)");   //
    const $logintime = $("input:eq(6)");   //

    const $labelName = $("#label-name");   // 标签
    const $labelEmail = $("#label-email");  //
    const $labelPhone = $("#label-phone");  //
    const $labelPhone1 = $("#label-phone1"); //
    const $labelPasswd = $("#label-passwd"); //

    const $res = $("#res");

    findUser($("span:eq(1)"), $res, $phone, $labelPhone, $name, $passwd, $email, $phone1, $regtime, $logintime, $labelName, $labelPasswd, $labelEmail, $labelPhone1);

    {
        $name.bind('input propertychange', function () {
            test_ipt(testName, $name, $labelName);
        })
        $passwd.bind('input propertychange', function () {
            test_ipt(testPasswd, $passwd, $labelPasswd);
        })
        $email.bind('input propertychange', function () {
            test_ipt(testEmail, $email, $labelEmail);
        })
        $phone1.bind('input propertychange', function () {
            test_ipt(testPhone, $phone1, $labelPhone1);
        })
    }

    let updateUser = function () {

        if ((json.name === $name.val()) &&
            (json.password === $passwd.val()) &&
            (json.email === $email.val()) &&
            (json.phone === $phone1.val())) {
            layer.msg('还没有做任何修改呢');
            return false;
        }

        $phone1.val($phone1.val().trim());
        if (!test_ipt(testPhone, $phone1, $labelPhone1)) {
            layer.msg('请输入正确的手机号码');
            return false;
        }
        let windowId = layer.load();
        $.getJSON("/admin/views/user/update.do", {
            id: json.id,
            name: $name.val(),
            passwd: $passwd.val(),
            email: $email.val(),
            phone: $phone1.val(),
        }, function (data) {
            layer.close(windowId);
            layer.msg(data.message);
            if (data.status > 0) {
                $name.val("");
                $passwd.val("");
                $email.val("");
                $phone1.val("");
                $res.hide();
                json.waitingForSamePhone = false;
            }
        });
        return false;
    };

    // 点击密码两字显示密码
    {
        let windowId;
        let havenClickedOnce = false;// 点击过一次后就不再提示
        $labelPasswd.hover(function () {
            if (!havenClickedOnce) windowId = layer.tips("点我可以显示密码", "#label-passwd", {tips: 1, time: 0});
        }, function () {
            if (!havenClickedOnce && windowId !== undefined) layer.close(windowId);
            windowId = undefined;
        })
        $labelPasswd.click(function () {
            if (!havenClickedOnce) layer.close(windowId);
            havenClickedOnce = true;
            if ($passwd.prop('type') === 'password')
                $passwd.prop('type', 'text');
            else $passwd.prop('type', 'password');
        })
        let timeout;
        let flag1 = true;
        $passwd.hover(function () {
            if (flag1) timeout = setTimeout(function () {
                windowId = layer.tips("偷偷告诉你，点击右边密码两个字可以显示密码", "#passwd", {tips: 1, time: 5000});
                flag1 = false;
            }, 5000);
        }, function () {
            if (timeout !== undefined) clearTimeout(timeout);
            timeout = undefined;
            if (windowId !== undefined) layer.close(windowId);
            windowId = undefined;
        })
    }

    $("span:eq(3)").click(updateUser);

    $("#reset").click(function () {
        $name.val(json.name);
        $passwd.val(json.password);
        $email.val(json.email);
        $phone1.val(json.phone);
        test_ipt(testName, $name, $labelName);
        test_ipt(testPasswd, $passwd, $labelPasswd);
        test_ipt(testEmail, $email, $labelEmail);
        test_ipt(testPhone, $phone1, $labelPhone1);
    })
});